package io.plus.server.command.command_handler.group;

import io.plus.common.io;
import io.plus.common.packet.Group;
import io.plus.common.packet.User;
import io.plus.common.packet.group.JoinGroupNotifyRequestMessage;
import io.plus.common.packet.group.JoinGroupResponseMessage;
import io.plus.common.packet.group.JoinGroupResult;
import io.plus.common.packet.metadata.Command;
import io.plus.common.packet.metadata.ResponseMessage;
import io.plus.common.protocol.Packet;
import io.plus.engine.pulse.AsynchronousSocketChannelContext;
import io.plus.server.SessionContext;
import io.plus.server.Status;
import io.plus.server.UserConfiguration;
import io.plus.server.command.CommandHandler;
import io.plus.server.utils.ImKit;
import io.plus.server.utils.JsonKit;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;


/**
 * 加入群组消息cmd命令处理器
 */
@Slf4j
@Data
public class JoinGroupCommandHandler implements CommandHandler {

    /**
     * 绑定群组
     */
    public Packet bindGroup(UserConfiguration userConfiguration, Packet packet, AsynchronousSocketChannelContext asynchronousSocketChannelContext) throws Exception {
        if (packet.getBody() == null) {
            throw new IllegalArgumentException("body is null");
        }
        Group group = JsonKit.toBean(packet.getBody(), Group.class);
        String groupId = group.getId();


        io.bindGroup(asynchronousSocketChannelContext, groupId);


        JoinGroupResponseMessage joinGroupResponseMessage = new JoinGroupResponseMessage();
        joinGroupResponseMessage.setGroupId(groupId);
        JoinGroupResult joinGroupResult = JoinGroupResult.allow_join;
        joinGroupResponseMessage.setJoinGroupResult(joinGroupResult);

        ResponseMessage responseMessage = new ResponseMessage(Command.join_group_response, Status.C10011).setData(joinGroupResponseMessage);
        Packet respPacket = ImKit.convertResponsepPacket(asynchronousSocketChannelContext, responseMessage);
        return respPacket;
    }

    @Override
    public Command command() {

        return Command.join_group_request;
    }

    @Override
    public Packet handler(UserConfiguration userConfiguration, AsynchronousSocketChannelContext asynchronousSocketChannelContext, Packet packet) throws Exception {
        //绑定群组;
        Packet bindGroup = bindGroup(userConfiguration, packet, asynchronousSocketChannelContext);
        //发送进房间通知;
        joinGroupNotify(packet, asynchronousSocketChannelContext);
        return bindGroup;
    }

    /**
     * 发送进房间通知;
     *
     * @param packet
     * @param asynchronousSocketChannelContext
     */
    public void joinGroupNotify(Packet packet, AsynchronousSocketChannelContext asynchronousSocketChannelContext) {
        SessionContext sessionContext = (SessionContext) asynchronousSocketChannelContext.getAttribute();

        User clientUser = sessionContext.getClient().getUser();
        User notifyUser = new User(clientUser.getId(), clientUser.getNick());

        Group joinGroup = JsonKit.toBean(packet.getBody(), Group.class);
        String groupId = joinGroup.getId();
        //发进房间通知  COMMAND_JOIN_GROUP_NOTIFY_RESP
        JoinGroupNotifyRequestMessage joinGroupNotifyRequestMessage = new JoinGroupNotifyRequestMessage().setGroup(groupId).setUser(notifyUser);
        ResponseMessage notifyRespBody = new ResponseMessage(Command.join_group_notify_response, Status.ok, joinGroupNotifyRequestMessage);

        Packet joinGroupNotifyRespPacket = new Packet(Command.join_group_notify_response, Status.ok, notifyRespBody.toByte());
        io.sendToGroup(groupId, joinGroupNotifyRespPacket);
    }
}
